import { Component, OnInit } from '@angular/core';
import { DatePipe } from '@angular/common'

declare var form: any;

@Component({
  templateUrl: './indexdata.component.html',
  providers: [DatePipe]
})
export class IndexdataComponent implements OnInit {

  fs: any = null;
  sideDic: any = null;
  // 字典
  public dicTypeDic: any = null;
  public administrativeDic: any = null;
  public monthDic: any = null;

  constructor(private datePipe: DatePipe) {
    this.fs = new form.FormSideSimpleService('indexdata', {
      orderObject: { id: 0 },
      keyField: 'id',
      randomFields: ['id'],
      foreignKey: 'indexid'
    });

    this.sideDic = new form.DictionaryService('indexclummn');
    this.sideDic.loadAll();
    this.dicTypeDic = new form.DictionaryService('IndexDataDictionary');
    this.administrativeDic = new form.DictionaryService('p_administrative', this.filter);
    this.administrativeDic.loadAll();
    this.monthDic = new form.DictionaryService('report_month');
  }

  ngOnInit(): void {
  }

  filter(data) {
    if (data) {
      const ri = [];
      for (let i = data.length - 1; i >= 0; i--) {
        const e = data[i];
        if (e.info && e.info === '2') {
          ri.push(i);
        }
      }
      ri.forEach((j) => {
        data.splice(j, 1);
      });
    }
    return data;
  }


  saveForm() {
    let myYear = this.datePipe.transform(this.fs.er.reportYear,"yyyy");
    // console.log(myYear)
    this.fs.er.year = myYear
    this.fs.save();
  }
  
  editReport(item){
    if (item.year != null){
      let myDate = item.year+"/1/1"
      item.reportYear = new Date(myDate)
    }
    // console.log(item.year)
    this.fs.editRow(item)
  }
}
